// pages/game/rank.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBottom: false,
    page: 1,
    pageSize: 20,
    list: [],
    showTip: false,
    isToday: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.isBottom = false
    this.getToday()
  },
  toggleOption (e) {
    let that = this
    let id = e.target.dataset.id
    if (id == '1') {
      that.setData({
        isToday: true,
        list: [],
        page: 1
      })
      that.getToday()
    } else {
      that.setData({
        isToday: false,
        list: [],
        page: 1
      })
      that.getData()
    }
  },
  getData() {
    let that = this
    wx.showLoading({
      title: '正在加载数据'
    })
    app.request(app.apiDomain + 'index.php?c=games&a=getColorsRanking', {
      page: that.data.page,
      page_size: that.data.pageSize
    }, function (res) {
      wx.hideLoading()
      if (res.data.status == 1) {
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '数据已经到底了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            isBottom: true,
            showTip: true
          })
          return false
        } else if (res.data.data.list.length <= that.data.pageSize){
          let list = that.data.list.concat(res.data.data.list)
          let user = res.data.data.user.hasOwnProperty('id') ? res.data.data.user : ''
          let rank = parseInt(res.data.data.ranking) + 1
          if (res.data.data.user.length == 0) {
            that.setData({
              list: list,
              ranking: rank,
              isBottom: false
            })
          } else {
            that.setData({
              list: list,
              user: user,
              ranking: rank,
              isBottom: false
            })
          }
        }
      }
    }, 'POST')
  },
  getToday() {
    let that = this
    wx.showLoading({
      title: '正在加载数据'
    })
    app.request(app.apiDomain + 'index.php?c=games&a=getColorsTodayRanking', {
      page: that.data.page,
      page_size: that.data.pageSize
    }, function (res) {
      wx.hideLoading()
      if (res.data.status == 1) {
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '数据已经到底了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            isBottom: true,
            showTip: true
          })
          return false
        } else if (res.data.data.list.length <= that.data.pageSize) {
          let list = that.data.list.concat(res.data.data.list)
          let user = res.data.data.user.hasOwnProperty('id') ? res.data.data.user : ''
          let rank = parseInt(res.data.data.ranking) + 1
          if (res.data.data.user.length == 0) {
            that.setData({
              list: list,
              ranking: rank,
              isBottom: false
            })
          } else {
            that.setData({
              list: list,
              user: user,
              ranking: rank,
              isBottom: false
            })
          }
        }
      }
    }, 'POST') 
  },
  goto () {
    wx.redirectTo({
      url: 'color_index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.SystemInfo(this)
  },
  // 滑倒底部加载
  scrollBottom () {
    // console.log('到达底部')
    let that = this
    if (that.data.isBottom) {
      return false
    }
    that.isBottom = true
    that.data.page++
    if (that.data.isToday) {
      that.getToday()
    } else {
      that.getData()
    }
  },
  return_index () {
    wx.redirectTo({
      url: 'color_index',
    })
  },
  return_act () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})